const axios = require("axios")
const fs = require("fs")

// console.log(axios)

axios
  .post("http://inewgirl.com/api/embed/topLikedPost", {
    data: {
      count: 4,
      rand: 0,
      vip: 0,
    },
  })
  .then(res => {
    const source_data = fs.readFileSync("./allVipResponse.json", "utf8")
    const list_data = JSON.parse(source_data)
    const { list } = res.data.data
    let newList = []
    list.forEach(l => {
      if (list_data.findIndex(v => v.id === l.id) === -1) {
        list_data.push(l)
        newList.push(l.title)
      }
    })
    if (newList.length !== 0) {
      fs.writeFile(
        "./allVipResponse.json",
        JSON.stringify(list_data),
        { encoding: "utf-8" },
        err => {
          if (err) throw err
          console.log(`新增${newList.length}套图：`)
          console.log(newList)
        }
      )
    }
    console.log(`新增${newList.length}套图：`)
  })
  .catch(err => {
    console.log(err.message)
  })
